#include "./../inc/sequence_list.hpp"

sequence_list * SEQUENCE_LIST::create_sequence_list(void)
{
    sequence_list * sequence_list_variable = new sequence_list;
    if(NULL != sequence_list_variable)
    {   
        memset(sequence_list_variable,0,sizeof(sequence_list));
        return sequence_list_variable;
    }
    else
    {   
        return NULL;
    }
}

int SEQUENCE_LIST::add_sequence_list_data(sequence_list * sequence_list_variable ,int add_mode, DATA_TYPE data)
{   
    if(NULL == sequence_list_variable)
    {
        return POINTER_IS_NULL_ERROR;
    }
    if (SEQUENCE_LIST_SIZE == sequence_list_variable->count)
    {
        return SEQUENCE_LIST_IS_FULL;
    }
    
    switch(add_mode)
    {
        case HEAD_INSERT :
            for(int i = 0 ; i < sequence_list_variable->count ;i++)
            {
                sequence_list_variable->data[sequence_list_variable->count - i] = sequence_list_variable->data[sequence_list_variable->count-i-1];
            }
            sequence_list_variable->data[0] = data;
            sequence_list_variable->count++;
            break;
        case TRAIL_INSERT :
            sequence_list_variable->data[sequence_list_variable->count] = data;
            sequence_list_variable->count++;
            break;
        default:
            if(add_mode > sequence_list_variable->count || add_mode < 0)
            {
                return ADD_SEQUENCE_LIST_POSITION_FAULT;
            }
            sequence_list_variable->data[add_mode] = data;
            sequence_list_variable->count++;
            break;
    }
    return ADD_SEQUENCE_LIST_DATA_OK;
}
int SEQUENCE_LIST::amend_sequence_list(sequence_list * sequence_list_variable , int add_mode, DATA_TYPE data)
{
    if(NULL == sequence_list_variable)
    {
        return POINTER_IS_NULL_ERROR;
    }
    if ((SEQUENCE_LIST_SIZE - SEQUENCE_LIST_SIZE) == sequence_list_variable->count)
    {
        return SEQUENCE_LIST_IS_NULL;
    }
    switch(add_mode)
    {
        case HEAD_INSERT :
            sequence_list_variable->data[0] = data;
            break;
        case TRAIL_INSERT :
            sequence_list_variable->data[sequence_list_variable->count-1] = data;
            break;
        default:
            if(add_mode >= sequence_list_variable->count || add_mode < 0)
            {
                return AMEND_SEQUENCE_LIST_POSITION_FAULT;
            }
            sequence_list_variable->data[add_mode] = data;
            break;
    }
    return AMEND_SEQUENCE_LIST_DATA_OK;
}
int SEQUENCE_LIST::delete_sequence_list (sequence_list * sequence_list_variable ,int add_mode)
{
    if(NULL == sequence_list_variable)
    {
        return POINTER_IS_NULL_ERROR;
    }
    if ((SEQUENCE_LIST_SIZE - SEQUENCE_LIST_SIZE) == sequence_list_variable->count)
    {
        return SEQUENCE_LIST_IS_NULL;
    }
    switch(add_mode)
    {
        case HEAD_INSERT :
            for(int i = 0 ; i < (sequence_list_variable->count-1);i++)
            {
                sequence_list_variable->data[i] = sequence_list_variable->data[i+1];
            }
            sequence_list_variable->data[sequence_list_variable->count-1] = 0;
            sequence_list_variable->count--;
            break;
        case TRAIL_INSERT :
            sequence_list_variable->data[sequence_list_variable->count-1] = 0;
            sequence_list_variable->count--;
            break;
        default:
            if(add_mode >= sequence_list_variable->count || add_mode < 0)
            {
                return ADD_SEQUENCE_LIST_POSITION_FAULT;
            }
            for(int i = add_mode ; i < (sequence_list_variable->count-1);i++)
            {
                sequence_list_variable->data[i] = sequence_list_variable->data[i+1];
            }
            sequence_list_variable->data[add_mode] = 0;
            sequence_list_variable->count--;
            break;
    }
    return DELETE_SEQUENCE_LIST_DATA_OK;
}
int SEQUENCE_LIST::show_sequence_list(sequence_list * sequence_list_variable)
{
    if(NULL == sequence_list_variable)
    {
        return POINTER_IS_NULL_ERROR;
    }
    for(int i = 0 ;i < sequence_list_variable->count ;i++)
    {
        std::cout << "data[" << i <<"] = " << sequence_list_variable->data[i] << std::endl;
    }
    return SHOW_SEQUENCE_LIST_OK;
}
